/*
 * Name: Minh-Tuan ta
 * Class: UploadPhoto.java
 */

package actions;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import beans.*;
import data.*;

public class UploadPhoto extends Action {

	@Override
	protected void setURLMapping() {
		// TODO Auto-generated method stub
		urlMapping = "/UploadPhoto";
	}

	@Override
	protected String doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("User");
		String strAlbumID = request.getParameter("albumID");
		String url = "/WEB-INF/views/content/UploadPhoto.jsp";
		
		if (strAlbumID == null)
		{
			url = null;
		}
		else
		{
			Album album = AlbumDB.getAlbum(Integer.parseInt(strAlbumID));
			
			if (user.getUserID() != album.getUserID())
			{
				url = null;
			}
			else
			{
				request.setAttribute("album", album);
				request.setAttribute("albumID", album.getContentID());
			}
		}
		
		return url;
	}

	@Override
	protected String doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String fileName = "";
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("User");
		ServletContext context = session.getServletContext();
		String realContextPath = context.getRealPath("/");
		
		//Are we Processing File upload request
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if(isMultipart){
			fileName = utils.Utils.doUpload(request,realContextPath);
		}
		
		if (fileName == null) //path is empty
		{
			session.setAttribute("ReturnStatus", "Photo Not Uploaded");
		}
		else
		{
			Photo photo = new Photo();
			photo.setContent("");
			photo.setContentTitle("");
			photo.setContentTypeID(ContentTypeDB.getContentTypeID("Photo"));
			photo.setDateCreated(new Timestamp(new java.util.Date().getTime()));
			photo.setExtra(Integer.parseInt((String)request.getAttribute("albumID")));
			photo.setUserID(user.getUserID());
			int photoStatus = PhotoDB.createPhoto(photo);
			
			Resource resource = new Resource();
			resource.setContentID(photo.getContentID());
			resource.setResourceName("Photo");
			resource.setPath("images/users/" + fileName);
			
			//make resource
			int uploadStatus = ResourceDB.addResource(resource);
			
			if (photoStatus > 0)
			{
				session.setAttribute("ReturnStatus", "<p>Photo Created</p>");
				
				if (uploadStatus > 0)
				{
					session.setAttribute("ReturnStatus", "<p>Upload Successfully</p>");
				}
				else
				{
					session.setAttribute("ReturnStatus", "<p>Upload Failed</p>");
				}
			}
			else
			{
				session.setAttribute("ReturnStatus", "<p>Photo Not Created</p>");
			}
		}
		
		return "Photos?albumID=" + (String)request.getAttribute("albumID");
	}

	@Override
	protected void setupPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

}
